package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 * 此类是为了验证排他网关的默认顺序流，没有条件的顺序流是默认true顺着该顺序流继续扭转，还是所有其他有条件的顺序流都为false后默认走没有条件的顺序流
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/exclusiveGatewayb")
public class Camunda04exclusiveGatewayb {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/exclusiveGatewayb/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/04exclusiveGatewayB.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/exclusiveGatewayb/startProcess/[businessKey:gateway002]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){

        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("exclusiveGatewayb",businessKey);
        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]

    {
        "a": 1,
        "b": 1,
        "c": 2,
        "d": 1
    }
     */

    // 然后查看哪个任务被触发了

}
